package week01

// 20. 有效的括号
// https://leetcode-cn.com/problems/valid-parentheses/

// 栈 - 最近相关性

func isValid(s string) bool {
	m := map[byte]byte{
		')': '(',
		']': '[',
		'}': '{',
	}

	var stack []byte
	for _, b := range []byte(s) {
		switch b {
		case '(', '[', '{':
			stack = append(stack, b)
		default:
			if len(stack) == 0 {
				return false
			}
			if stack[len(stack)-1] != m[b] {
				return false
			}
			stack = stack[:len(stack)-1]
		}
	}
	return len(stack) == 0
}
